package com.bdqn.schoolcms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.schoolcms.constants.Constants;
import com.bdqn.schoolcms.domain.Dept;
import com.bdqn.schoolcms.domain.User;
import com.bdqn.schoolcms.dto.ClassDto;
import com.bdqn.schoolcms.mapper.UserMapper;
import com.bdqn.schoolcms.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.schoolcms.domain.SysClass;
import com.bdqn.schoolcms.mapper.SysClassMapper;
import com.bdqn.schoolcms.service.SysClassService;
import org.springframework.util.CollectionUtils;

@Service
public class SysClassServiceImpl implements SysClassService{

    @Autowired
    private SysClassMapper sysClassMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public DataGridView listPage(ClassDto classDto) {
        Page<SysClass> page=new Page<>(classDto.getPageNum(),classDto.getPageSize());
        QueryWrapper<SysClass> qw=new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(classDto.getClassName()),SysClass.COL_CLASS_NAME,classDto.getClassName());
        qw.orderByDesc(SysClass.COL_CREATE_TIME);
        this.sysClassMapper.selectPage(page,qw);
        List<SysClass> records = page.getRecords();
        if (!CollectionUtils.isEmpty(records)){
            records.forEach(item ->{
                Integer integer = userMapper.selectCount(new QueryWrapper<User>().lambda().eq(User::getStudentClass, item.getClassId()));
                item.setClassCount(integer.toString());
            });
        }
        page.setRecords(records);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public List<SysClass> list() {
        QueryWrapper<SysClass> qw=new QueryWrapper<>();
        qw.eq(SysClass.COL_STATUS, Constants.STATUS_TRUE);
        return this.sysClassMapper.selectList(qw);
    }

    @Override
    public SysClass getOne(Long classId) {
        return this.sysClassMapper.selectById(classId);
    }

    @Override
    public int addClass(ClassDto classDto) {
        SysClass sysClass=new SysClass();
        BeanUtil.copyProperties(classDto,sysClass);
        sysClass.setClassCount("0");
        //设置创建人，创建时间
        sysClass.setCreateTime(DateUtil.date());
        sysClass.setCreateBy(classDto.getSimpleUser().getUserName());
        return this.sysClassMapper.insert(sysClass);
    }

    @Override
    public int updateClass(ClassDto classDto) {
        SysClass sysClass=new SysClass();
        BeanUtil.copyProperties(classDto,sysClass);
        //设置修改人
        sysClass.setUpdateBy(classDto.getSimpleUser().getUserName());
        return this.sysClassMapper.updateById(sysClass);
    }

    @Override
    public int deleteClassByIds(Long[] classIds) {
        List<Long> ids = Arrays.asList(classIds);
        if(ids!=null&&ids.size()>0){
            return this.sysClassMapper.deleteBatchIds(ids);
        }
        return 0;
    }
}
